﻿using System;

namespace YXYLCommon
{
    [Serializable]
    public class DbInt
    {
        private DbInt(object value)
        {
            if (value == null)
            {
                this.dvalue = dvalueDBNull;
                this.dvalueDBParameter = Convert.DBNull;
            }
            else if(value.GetType().Name=="DbInt")
            {
                this.dvalue = ((DbInt)value).Value;
                this.dvalueDBParameter = this.dvalue;
            }
            else if (value == Convert.DBNull || Convert.ToString(value) == "")
            {
                this.dvalue = dvalueDBNull;
                this.dvalueDBParameter = Convert.DBNull;
            }
            else
            {
                this.dvalue = Convert.ToInt32(value);
                this.dvalueDBParameter = this.dvalue;
            }
        }
        private DbInt(string value)
        {
            int valueTemp;
            if (value == string.Empty || value == null || int.TryParse(value, out valueTemp) == false)
            {
                this.dvalue = dvalueDBNull;
                this.dvalueDBParameter = Convert.DBNull;
            }
            else
            {
                this.dvalue = valueTemp;
                this.dvalueDBParameter = valueTemp;
            }
        }
        private DbInt(int value)
        {
            this.dvalue = value;
            this.dvalueDBParameter = value;
        }

        private object dvalueDBParameter = Convert.DBNull;
        private int dvalue = dvalueDBNull;
        private static readonly int dvalueDBNull = int.MinValue;

        public int Value
        {
            get { return dvalue; }
            set { dvalue = value; }
        }
        /// <summary>
        /// 用于执行存储过程需要的参数
        /// </summary>
        public object DvalueDBParameter
        {
            get { return dvalueDBParameter; }
        }


        public static DbInt InitNull()
        {
            return new DbInt(null);
        }
        public bool IsDBNull
        {
            get
            {
                if (this.dvalue == dvalueDBNull)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public override string ToString()
        {
            string str = string.Empty;
            if (this.dvalue != dvalueDBNull)
            {
                str = this.dvalue.ToString();
            }
            return str;
        }

        public string ToString(string format)
        {
            if (this.dvalue == dvalueDBNull)
            {
                return string.Empty;
            }
            else
            {
                return this.dvalue.ToString(format);
            }
        }
        public string ValueString
        {
            get
            {
                if (this.dvalue == dvalueDBNull)
                {
                    return string.Empty;
                }
                else
                {
                    return this.dvalue.ToString();
                }
            }
        }

        public static DbInt Parse(object value)
        {
            return new DbInt(value);
        }
        public static DbInt Parse(string value)
        {
            return new DbInt(value);
        }
        public static DbInt Parse(int value)
        {
            return new DbInt(value);
        }

        public override bool Equals(object obj)
        {
            if (obj.GetType().Name == "Int32" || obj.GetType().Name == "Double" || obj.GetType().Name == "Decimal")
            {
                return this.Value == Convert.ToInt32(obj);
            }
            else if (obj.GetType().Name == "DbInt")
            {
                return this.Value == ((DbInt)obj).Value;
            }
            else
            {
                return false;
            }
        }


    }

}
